package com.learn.algorithm.stack;

import java.util.Stack;

public class StackGetMin extends MyStack<Integer> {
    private MyStack<Integer> stackData;
    private MyStack<Integer> stackMin;

    public StackGetMin() {
        this.stackData = new MyStack<>();
        this.stackMin = new MyStack<>();
    }

    @Override
    public void push(Integer Item) {
        if (this.stackMin.isEmpty()) {
            this.stackMin.push(Item);
        } else if (Item <= this.getMin()) {
            this.stackMin.push(Item);
        }
        this.stackData.push(Item);
    }

    @Override
    public Integer pop() {
        if (stackData.isEmpty()) {
            throw new RuntimeException("you stack is empty");
        }
        Integer value = stackData.pop();
        if (value == stackMin.peek()) {
            stackMin.pop();
        }
        return value;
    }

    public Integer getMin() {
        if (this.stackMin.isEmpty()) {
            throw new RuntimeException(" your stack is empty");
        }
        return this.stackMin.peek();
    }
}
